package com.sunfeng.linkedlist;

import javax.xml.bind.SchemaOutputResolver;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 尾插法链表的实现
 */
public class SingleLinkedListDemo {

    public static void main(String[] args) {
        new LinkedList<String>();
        Stack<String> objects = new Stack<String>();
        SingLinkedList linkedList = new SingLinkedList();
        HeroNode heroNode1 = new HeroNode(1, "松江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        linkedList.add(heroNode1);
        linkedList.add(heroNode2);
        linkedList.add(heroNode3);
        linkedList.add(heroNode4);
        linkedList.list();
//        HeroNode heroNode5 = new HeroNode(1, "林冲1", "豹子头");
//        linkedList.edit(heroNode5);


        // System.out.println("翻转");
        // reverseNode(linkedList.getHead());
        //  linkedList.list();
        System.out.println("翻转打印");
        reversePrint(linkedList.getHead());

        // HeroNode heroNode5 = new HeroNode(4, "林冲1", "豹子头");
//        linkedList.addByOrder(heroNode3);
//        linkedList.addByOrder(heroNode4);
//        linkedList.addByOrder(heroNode2);
//        linkedList.addByOrder(heroNode1);
//        linkedList.delete(2);
//        linkedList.list();
//        System.out.println("链表的长度为：" + getNodeLength(linkedList.getHead()));
//        System.out.println("获取的结果为：" + getLastIndexNode(linkedList.getHead(), 3));


    }

    /**
     * 实现倒序打印
     *
     * @param head
     */
    public static void reversePrint(HeroNode head) {
        Stack<HeroNode> heroNodeStack = new Stack<>();

        HeroNode cur = head.next;
        while (cur != null) {
            heroNodeStack.push(cur);
            cur = cur.next;
        }

        while (heroNodeStack.size()>0){
            System.out.println(heroNodeStack.pop());
        }


    }

    /**
     * 翻转链表
     *
     * @param head
     */
    public static void reverseNode(HeroNode head) {

        if (head.next == null || head.next.next == null) {
            return;
        }
        //定义一个辅助指针
        HeroNode cur = head.next;
        //1、先定义一个节点 保存新的数据
        // 指向当前节点的下一个节点
        HeroNode next = null;
        HeroNode reverseNode = new HeroNode(0, "", "");
        //2、从头到尾遍历原先的节点 每遍历一次就将其取出  并放在新节点的头部
        while (cur != null) {
            // 先暂时保存当前节点的下一个节点，因为后面需要使用    cur = next;
            next = cur.next;
            //将cur的下一个节点指向新的reverseHead的第一个节点
            cur.next = reverseNode.next;
            //新的头节点指向cur节点，将cur连接到新的链表
            reverseNode.next = cur;
            // 让cut后移
            cur = next;
        }
        //3、 原先的链表 head.next 只想reverseHead.next
        head.next = reverseNode.next;

    }


    /**
     * 获取倒数第几个位置节点
     *
     * @param head
     * @param index
     * @return
     */
    public static HeroNode getLastIndexNode(HeroNode head, int index) {
        if (head.next == null) {
            return null;
        }
        int nodeLength = getNodeLength(head);
        HeroNode cur = head.next;

        for (int i = 0; i < nodeLength - index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    /**
     * 获取链表的长度
     *
     * @param head 链表的头结点
     * @return
     */
    public static int getNodeLength(HeroNode head) {
        if (head.next == null) {
            return 0;
        }
        int count = 0;
        HeroNode cur = head.next;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


}

// 定义列表
class SingLinkedList {
    //先初始化头结点
    private HeroNode head = new HeroNode(0, "", "");

    //思路，不考虑编号顺序时
    //1、找到当前节点的最后节点
    //2、最后的节点的next指向新节点   尾插法
    public void add(HeroNode node) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
    }

    //第二种方式在添加英雄时， 根据排名将英雄插入到指定位置
    //(如果有这个排名， 则添加失败， 并给出提示)
    public void addByOrder(HeroNode node) {
        //因为头节点不能动， 因此我们仍然通过一个辅助指针(变量)来帮助找到添加的位置
        //因为单链表， 因为我们找的 temp 是位于 添加位置的前一个节点， 否则插入不了
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > node.no) {
                break;
            }
            if (temp.next.no == node.no) {
                flag = true;
                break;
            }
            // 都不满足寻找temp的下一个节点，并赋值给temp
            temp = temp.next;
        }
        if (flag) {
            System.out.println("已经有此排名，添加失败");
        } else {
            node.next = temp.next;
            temp.next = node;
        }
    }

    /**
     * 完成修改节点  根据节点no进行修改
     *
     * @param node
     */
    public void edit(HeroNode node) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {

            }
            if (temp.no == node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = node.name;
            temp.nickName = node.nickName;
        } else {
            System.out.println("未找到要修改的元素");
        }

    }

    /**
     * 单链表删除
     *
     * @param no
     */
    public void delete(int no) {

        HeroNode temp = head;
        boolean flag = false;
        while (true) {

            if (temp.next == null) {
                System.out.println("链表结束");
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.println("未找到要删除的元素");
        }


    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        // 头结点不能动
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public HeroNode getHead() {
        return head;
    }


}


// 定义数据节点
class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;
    //构造器

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +

                '}';
    }
}
